﻿import _ko = require('knockout');

import QuestionInfo = require('entities/questioninfo');

class ArgumentInfo extends QuestionInfo {
    public referenceId: string = '-1';
    public type: KnockoutObservable<string> = _ko.observable<string>('');
    public onTypeChange: (source: any, data: any) => void = null;

    constructor(_question: QuestionInfo, _post: Disco.Ontology.Post) {
        super(_post);

        if (_post.RefersTo) {
            var refersTo: Disco.Ontology.PostReference[] =
                _post.RefersTo.filter(post => post.ReferreeId == _question.questionId);

            var conArguments: Disco.Ontology.PostReference[] =
                refersTo.filter(post => post.ReferenceTypeId == '6' || post.ReferenceTypeId == '8');
            if (conArguments.length > 0) {
                this.type('con');
            } else {
                var proArguments: Disco.Ontology.PostReference[] =
                    refersTo.filter(post => post.ReferenceTypeId == '5' || post.ReferenceTypeId == '7');
                if (proArguments.length > 0) {
                    this.type('pro');
                }
            }

            // HINT: We need to determine what kind of relation already exists between the question (referree) and its arguments (referrer).
            //       There might be *indirect* references like 'Evidence' and 'Objection' or *direct* ones like 'Agreement' or 'Disagreement'.
            //       And there might also be *unspecific* ones like 'General' or 'Child'.
            //       Question now is: Should we override existing references in general? Or should we better distinguish between different reference types?
            //       For now the implementation solely do change *well known* reference types like 'Agreement', 'Disagreement' and 'General'!
            //       If there is any other kind of reference, they will stay untouched and a new reference will be created.
            //
            //       That might become troublesome, at least when it comes to resetting the references! But we will see...
            //
            //       Maybe we should better process solely the well known ones? But in general the question is how to deal with multiple references?
            //       We need to discuss the concept of references soon. I would say it makes no sense to to have multiple references of the same type between the same entities.
            //       That said if given post A and post B there should be only one reference of type 'Agreement'. 
            //       And at the same time it makes no sense to have a reference of type 'Disagreement' here.Hence the reference types 'Agreement' and 'Disagreements' are exclusive here.
            //       But what if between post A and B having one reference of type 'Agreement' and another one of type 'Objection'?

            var discoArguments: Disco.Ontology.PostReference[] =
                refersTo.filter(post => post.ReferenceTypeId == '7' || post.ReferenceTypeId == '8');
            if (discoArguments.length > 0) {
                this.referenceId = discoArguments[0].Id;
            } else {
                var generalArguments: Disco.Ontology.PostReference[] =
                    refersTo.filter(post => post.ReferenceTypeId == '2');
                if (generalArguments.length > 0) {
                    this.referenceId = generalArguments[0].Id;
                }
            }
        }
        this.type.subscribe((newValue: string) => {
            if (this.onTypeChange) {
                this.onTypeChange(this, newValue);
            }
        });
    }
}

export = ArgumentInfo;